package DAO;

import hibernate.AssetMaintainInfo;
import hibernateUtil.HibernateUtil;

import java.sql.Date;
import java.util.List;
import java.util.Vector;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class ZCWX {
	
	private int totalByQuery = 0;
	
	/*
	 * save资产报损信息
	 */
	public void addAssetWx(String parameter[])
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		AssetMaintainInfo assetMaintainInfo = new AssetMaintainInfo();
		
		double laborageMoney = Double.parseDouble(parameter[11]);
		double materialMoney = Double.parseDouble(parameter[10]);		
		double maintainMoney = Double.parseDouble(parameter[5]);
		int days = Integer.parseInt(parameter[4]);
		Date time = java.sql.Date.valueOf(parameter[8]);
		
		assetMaintainInfo.setAssetNo(parameter[0]);
		assetMaintainInfo.setModel(parameter[1]);
		assetMaintainInfo.setLevel(parameter[2]);
		assetMaintainInfo.setMaterial(parameter[3]);
		assetMaintainInfo.setDays(days);
		assetMaintainInfo.setMaintainMoney(maintainMoney);
		assetMaintainInfo.setName(parameter[6]);
		assetMaintainInfo.setDepartment(parameter[7]);
		assetMaintainInfo.setTime(time);
		assetMaintainInfo.setConstructUnit(parameter[9]);
		assetMaintainInfo.setMaterialMoney(materialMoney);
		assetMaintainInfo.setLaborageMoney(laborageMoney);
		assetMaintainInfo.setReason(parameter[12]);
		assetMaintainInfo.setRemark(parameter[13]);
		
		session.save(assetMaintainInfo);
		trans.commit();		
	}
	
    /*
     * 得到资产维修信息的总记录
     */
    public int getTotal()
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		int countTotal = (session.createQuery("from AssetMaintainInfo").list()).size(); //资产信息的总数;
		
		trans.commit();
		
		return countTotal;
    }
    
    /*
     * 查询的资产维修信息的总记录
     */
    public int getTotalByQuery()
    {
    	return totalByQuery;
    }    
    
    @SuppressWarnings("unchecked")    
    /*
     * 通过参数得到资产维修的所有信息
     * 参数 start ， limit
     */
	public List getAllAssetWx(int start , int limit)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();

		Query query = session.createQuery("from AssetMaintainInfo");
		query.setFirstResult(start);
		query.setMaxResults(limit);
		List l = query.list();
		
		trans.commit();
		
		return l;
    } 
    
    @SuppressWarnings("unchecked")
    /*
     * 通过查询条件得到符合条件的资产维修信息
     * 参数 --- start ,limit ,assetNo , assetName , 
     *          useDepartment ,damageReason
     */
    public List getAssetWxByQuery(int start,int limit,String assetNo,String assetName,String useDepartment,String level)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction();	
		
		String str = "from AssetMaintainInfo where ";
		
		if(!"".equals(assetNo))
		{
			str = str + "assetNo=:assetNo ";
		}
		if(!"".equals(assetName))
		{
			if(!"".equals(assetNo))
			{
				str = str + "or name=:assetName ";
			}
			else
			{
				str = str + "name=:assetName ";
			}
		}
		if(!"".equals(useDepartment))
		{
			if(!"".equals(assetNo) || !"".equals(assetName))
			{
				str = str + "or department=:useDepartment ";
			}
			else
			{
				str = str + "department=:useDepartment ";
			}
		}
		if(!"".equals(level))
		{
			if(!"".equals(assetNo) || !"".equals(assetName) || !"".equals(useDepartment))
			{
				str = str + "or level=:level ";
			}
			else
			{
				str = str + "level=:level ";
			}
		}
		
		Query query = session.createQuery(str);    //查询数据库
		
		if(!"".equals(assetNo))
		{
			query.setString("assetNo", assetNo);
		}
		if(!"".equals(assetName))
		{
			query.setString("assetName", assetName);
		}
		if(!"".equals(useDepartment))
		{
			query.setString("useDepartment", useDepartment);
		}		
		if(!"".equals(level))
		{
			query.setString("level", level);
		}		
		
		totalByQuery = query.list().size(); //通过查询条件，得到资产信息的记录总数
		
		query.setFirstResult(start);    //查询记录从这一条开始；
		query.setMaxResults(limit); 	  //查询记录限制条数；
		List l = query.list();

		trans.commit();
		
		return l;
    }    
    
    @SuppressWarnings("unchecked")
    /*
     * 通过资产编号（assetNo）删除资产维修信息
     */
	public void deleteAssetWx(Vector assetNo)
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction(); 
		
		for(int i=0; i<assetNo.size(); i++ )
		{
			AssetMaintainInfo assetMaintainInfo = (AssetMaintainInfo)session.get(AssetMaintainInfo.class, assetNo.elementAt(i).toString());
			session.delete(assetMaintainInfo);
		}
		
		trans.commit();
    } 
    
    /*
     * 通过资产编号(assetNo)更新资产维修信息
     * 参数 
     */
    public void updataAssetWx(String assetWx[])
    {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction trans = session.beginTransaction(); 		
		
    	AssetMaintainInfo assetMaintainInfo = (AssetMaintainInfo)session.get(AssetMaintainInfo.class, assetWx[0]); 
    	
		double materialMoney = Double.parseDouble(assetWx[10]);
		double laborageMoney = Double.parseDouble(assetWx[11]);
		double maintainMoney = Double.parseDouble(assetWx[5]);
		int days = Integer.parseInt(assetWx[4]);
		Date time = java.sql.Date.valueOf(assetWx[8]);
		
		assetMaintainInfo.setModel(assetWx[1]);
		assetMaintainInfo.setLevel(assetWx[2]);
		assetMaintainInfo.setMaterial(assetWx[3]);
		assetMaintainInfo.setDays(days);
		assetMaintainInfo.setMaintainMoney(maintainMoney);
		assetMaintainInfo.setName(assetWx[6]);
		assetMaintainInfo.setDepartment(assetWx[7]);
		assetMaintainInfo.setTime(time);
		assetMaintainInfo.setConstructUnit(assetWx[9]);
		assetMaintainInfo.setMaterialMoney(materialMoney);
		assetMaintainInfo.setLaborageMoney(laborageMoney);
		assetMaintainInfo.setReason(assetWx[12]);
		assetMaintainInfo.setRemark(assetWx[13]);
		
		session.update(assetMaintainInfo);
		
		trans.commit();
    }    

}
